<template>
  <div class="app-container" style="height: 100%">
    <el-form ref="queryForm" :model="queryParams" size="mini" inline>
      <el-form-item prop="projectName">
        <el-input v-model.trim="queryParams.projectName" placeholder="请输入项目名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item prop="accidentTitle">
        <el-input v-model.trim="queryParams.accidentTitle" placeholder="请输入事故标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button size="mini" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        <el-button size="mini" type="primary" icon="el-icon-plus" @click="$refs.EditProAccident.open()" v-hasPermi="['baseManage:projectAccident:add']">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="listLoading" :data="list" size="small" height="calc(100% - 100px)">
      <el-table-column label="项目名称" prop="projectName" show-overflow-tooltip min-width="100" />
      <el-table-column label="事故标题" prop="accidentTitle" show-overflow-tooltip min-width="200" />
      <el-table-column label="事故内容描述" min-width="200">
        <el-popover slot-scope="{ row }" placement="top" trigger="click">
          <div style="max-width: 600px; white-space: pre-wrap">{{ row.accidentContent }}</div>
          <div slot="reference" class="ellipsis">{{ row.accidentContent }}</div>
        </el-popover>
      </el-table-column>
      <el-table-column label="事故等级" align="center" width="80">
        <dict-tag slot-scope="{ row }" :options="proAccidentLevels" :value="row.level" />
      </el-table-column>
      <el-table-column label="发生日期" prop="occurDate" align="center" width="100" />
      <el-table-column label="修改人" prop="updator" align="center" width="80" />
      <el-table-column label="最后修改时间" prop="updateTime" align="center" width="150" />
      <el-table-column label="操作" align="center" width="120" fixed="right">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="$refs.EditProAccident.open(row)" v-hasPermi="['baseManage:projectAccident:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" :loading="row.removeLoading" @click="handleDelete(row)" v-hasPermi="['baseManage:projectAccident:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <EditProAccident ref="EditProAccident" v-model="showEditProAccident" @ok="getList" />
  </div>
</template>
<script>
// 工程质量-项目事故
import { proAccidentLevels } from '@/utils/baseData'
import { listProAccident, delProAccident } from '@/api/qualityManage/cq'
import EditProAccident from './components/EditProAccident'
export default {
  name: 'ProAccident',
  components: { EditProAccident },
  data() {
    return {
      proAccidentLevels,
      listLoading: false,
      total: 0,
      list: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: undefined,
        accidentTitle: undefined
      },
      showEditProAccident: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      const res = await listProAccident(this.queryParams).catch(() => false)
      this.listLoading = false
      if (res === false) return

      const data = res.rows || []
      data.map(i => {
        i.removeLoading = false
      })
      this.list = data
      this.total = res.total || 0
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    async handleDelete(item) {
      if (item.removeLoading) return
      const res1 = await this.$confirm(`确认删除: ${item.accidentTitle}?`, '警告', { type: 'warning' }).catch(() => false)
      if (res1 !== 'confirm') return

      item.removeLoading = true
      const res = await delProAccident(item.accidentId).catch(() => false)
      item.removeLoading = false
      if (res === false) return

      this.msgSuccess('删除成功')
      this.list.length > 1 ? this.getList() : this.handleQuery()
    }
  }
}
</script>
